﻿using Kalman;
using Kalman.Extensions;
using Kalman.Maths;
using Kalman.Security;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using NLog;
using NLog.Fluent;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace QuDao
{
    /// <summary>
    /// 工具类
    /// </summary>
    public static class Utils
    {
        /// <summary>
        /// 从阿里云OSS地址中解析出对应的OssKey
        /// </summary>
        /// <param name="ossUrl">阿里云OSS地址</param>
        /// <param name="prifix">OssKey前缀</param>
        /// <returns></returns>
        public static string GetOssKey(string ossUrl, string prifix = "circle")
        {
            var idx = ossUrl.IndexOf($"/{prifix}/") + 1;
            if (idx == 0) return string.Empty; //非有效的Url直接返回空

            var key = ossUrl.Remove(0, idx);
            return key;
        }

        /// <summary>
        /// 将字符串数组转换为整数数组
        /// </summary>
        /// <param name="stringArray"></param>
        /// <returns></returns>
        /// <exception cref="AppException"></exception>
        public static int[] ConvertStringArrayToIntArray(string[] stringArray)
        {
            if (stringArray == null)
                return Array.Empty<int>();

            var intList = new List<int>();

            foreach (var item in stringArray)
            {
                if (int.TryParse(item, out int result))
                {
                    intList.Add(result);
                }
                else
                {
                    throw new AppException($"字符串\"{item}\"不是整数类型");
                }
            }

            return intList.ToArray();
        }

        public static string SendSms(Order order)
        {
            var url = "https://s.banan8.com/SmsAPI/SendSms";

            var client = new RestClient();
            var request = new RestRequest(url, Method.POST);

            try
            {
                ////var tenant = TenantBO.Instance.GetItem(order.TID);
                //var shop = ShopBO.Instance.GetItem(order.ShopID);
                //var mobile = shop.OwnerMobile;
                //var content = $"您有新的订单，请及时处理：{order.GoodsName.CutString(0,20)}-￥{order.OrderAmount}";
                //var cid = "103669";
                //var key = "40w1501vodu8e6vt4mai0th6ya9ewpch";
                //var sign = HashCryto.GetHash2String($"{cid}{content}{mobile}{key}", HashAlgorithmType.MD5, Encoding.UTF8);

                //request.AddParameter("cid", cid);
                //request.AddParameter("mobile", mobile);
                //request.AddParameter("content", content);
                //request.AddParameter("sign", sign);

                //var response = client.Execute(request);
                //if (!response.IsSuccessful)
                //{
                //    var msg = $"错误信息：{response.ErrorMessage}，内容详情：{response.Content}";
                //    throw new Exception(msg);
                //}

                //if (response.StatusCode == System.Net.HttpStatusCode.OK && !string.IsNullOrEmpty(response.Content))
                //{
                //    return response.Content;
                //}
                //else
                //{
                //    var msg = $"{(int)response.StatusCode}-{response.StatusDescription}";
                //    throw new Exception(msg);
                //}
                return "";
            }
            catch (Exception ex)
            {
                //var log = LogManager.GetCurrentClassLogger();
                //log.Error(ex);
                return ex.Message;
            }
        }
    }
}
